/***************************************************************************
 *  Copyright (C) 2009, 2010 by Axel Jaeger <axeljaeger@googlemail.com>    *
 *                                                                         *
 *  This file is part of Glowworm.                                         *
 *                                                                         *
 *  Glowworm is free software: you can redistribute it and/or modify       *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, version 3 of the License.                *
 *                                                                         *
 *  Glowworm is distributed in the hope that it will be useful,            *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with Glowworm.  If not, see <http://www.gnu.org/licenses/>.      *
 ***************************************************************************/

#include "FileServlet.h"

#include "HttpResponse.h"
#include "HttpRequest.h"

#include <QFileInfo>
#include <QDir>

FileServlet::FileServlet(HttpServer* parent) : AbstractServlet(parent)
{
}

FileServlet::~FileServlet()
{

}

QDir FileServlet::wwwRoot() const
{
    return m_wwwRoot;
}

void FileServlet::setWwwRoot(const QDir & dir)
{
    m_wwwRoot = dir;
}

void FileServlet::request(HttpRequest* request, HttpResponse* response)
{
    QFileInfo file_info(wwwRoot().path() + QDir::separator() +request->url().path());

    if (!file_info.exists()) {
        response->setStatusCode(HttpResponse::HttpFileNotFound);
        return;
    }

    if (file_info.isDir()) {
        QDir dir(file_info.filePath());
        QStringList filters;
        filters << "index.*";
        QStringList index_files(dir.entryList(filters));
        if (!index_files.isEmpty()) {
            QFileInfo index_info(file_info.absoluteFilePath() +  QDir::separator() + index_files.first());
            file_info = index_info;
        } else {
            response->setStatusCode(HttpResponse::HttpFileNotFound);
            return;
        }
    }

    QFile file(file_info.filePath());
    if (! file.open(QIODevice::ReadOnly)) {
        response->setStatusCode(HttpResponse::HttpForbidden);
    } else {
        response->setStatusCode(HttpResponse::HttpOK);
        response->setMimeType(mimeTypeForFile(file_info));
        response->sendHeader();
        response->outputDevice()->write(file.readAll());
    }
}

QString FileServlet::mimeTypeForFile(const QFileInfo & file)
{
    QString extension(file.suffix());
    QString mimeType("application/octet-stream");

    if (extension == "html" || extension == "htm") {
        mimeType = "text/html";
    } else if (extension == "png") {
        mimeType = "image/png";
    } else if (extension == "css") {
        mimeType = "text/css";
    } else if (extension == "js") {
        mimeType = "text/javascript";
    }


    return mimeType;
}
